# 作者 ：longxiaonan@aliyun.com
# 开发时间： 2022/11/25 20:34


# 作者 ：longxiaonan@aliyun.com
# 开发时间： 2022/11/25 0:52
import functools

from flask import Flask, redirect, session, url_for

app = Flask(__name__)

app.secret_key = 'abcdefg'

userMsg = 'userMsg'

def auth(func):
    @functools.wraps(func)
    def inner(*args, **kwargs):
        user = session.get('userMsg')
        if not user:
            return redirect(url_for('login'))
        return func(*args, **kwargs)
    return inner

# 通过自定义装饰器实现认证
# @app.route('/index')
# @auth
# def index():
#     return '首页'

# 特殊装饰器实现
@app.before_request
def f1():
    print('f1')
@app.before_request
def f2():
    print('f2')
@app.after_request
def f10(response):
    print('f10')
    return response
@app.after_request
def f10(response):
    print('f20')
    return response

@app.route('/index')
def index():
    return '首页'

@app.route('/add')
@auth
def add():
    return '添加'

@app.route('/login')
def login():
    session['userMsg'] = 'long:18'
    return redirect('/index')


if __name__ == '__main__':
    app.run()
